﻿using ConfExpert.Forms.Base;
using DevTeam;
using NewLand;
using System;
using System.Data;

namespace ConfExpert.Forms.Depo.Tasinma
{
    public partial class Giris : XForm
    {
        NewLandBarcode newLand = null;
        DataTable dtShelf = null;

        public Giris()
        {
            InitializeComponent();
            this.Text = "İrsaliye Kontrol";
        }

        private void Giris_Load(object sender, EventArgs e)
        {
            newLand = new NewLandBarcode(this);

            biCom1.EditValue = ConfExpert.Properties.Settings.Default.NewLand1;
            biCom2.EditValue = ConfExpert.Properties.Settings.Default.NewLand2;
            biCom3.EditValue = ConfExpert.Properties.Settings.Default.NewLand3;
            biCom4.EditValue = ConfExpert.Properties.Settings.Default.NewLand4;
            //Sıra önemli;
            this.biCom1.EditValueChanged += biCom_EditValueChanged;
            this.biCom2.EditValueChanged += biCom_EditValueChanged;
            this.biCom3.EditValueChanged += biCom_EditValueChanged;
            this.biCom4.EditValueChanged += biCom_EditValueChanged;

            if (biCom1.EditValue.ToInt() > 0)
                newLand.CihazEkle(1, biCom1.EditValue.ToInt());

            if (biCom2.EditValue.ToInt() > 0)
                newLand.CihazEkle(2, biCom2.EditValue.ToInt());

            if (biCom3.EditValue.ToInt() > 0)
                newLand.CihazEkle(3, biCom3.EditValue.ToInt());

            if (biCom4.EditValue.ToInt() > 0)
                newLand.CihazEkle(4, biCom4.EditValue.ToInt());

            if (newLand.Cihazlar.Count == 0)
                btnStart.Enabled = true;
            else
                btnStart.Enabled = false;

            newLand.BarcodeRead += cihaz_BarcodeRead;

            dtShelf = ExecuteDataTable("GetShelfs");


            gvIrsaliye.Init("GetIrsaliyeKontrolForDepoTransfer");
            gvIrsaliye.AddDetailGrid(gvTop, "KumasIrsaliyeId");

            gvIrsaliye.AddColumn("Adet", typeof(int), "COUNT(Child({FK}).RollId)");
            gvTop.AddColumn("E", typeof(int), "IIF(ShelfId=599, 1, 0)");
            gvIrsaliye.AddColumn("E", typeof(int), "SUM(Child({FK}).E)");

            gvIrsaliye.SetCaptions("E:Raflanmamış");
            gvIrsaliye.MakeColumnInvisible("KumasIrsaliyeId");
            gvIrsaliye.MakeReadOnly();

            gvTop.SetCaptions("Qty:SistemMetrajı");
            gvTop.MakeColumnInvisible("E");
            gvTop.MakeReadOnly();

            gvTop.FormatAsNumber(1, "Qty", "FizikselMetraj");
            gvTop.AddSummary("Qty", "FizikselMetraj");

            gvIrsaliye.BestFitColumns();
            gvTop.BestFitColumns();

            bbiGuncelle.ItemClick += bbiGuncelle_ItemClick;
        }

        void bbiGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvIrsaliye.RefreshDataSource("GetIrsaliyeKontrolForDepoTransfer");
        }

        void cihaz_BarcodeRead(int Barcode, DateTime Tarih, int CihazNo)
        {
            if (Barcode >= 1e9) //Raf barkodu
            {
                Barcode = Barcode - (1e9).ToInt();
                DataRow[] Rafs = dtShelf.Select("ShelfId=" + Barcode.ToStr());
                string raf = "TANIMSIZ RAF !";
                if (Rafs.Length > 0)
                    raf = Rafs[0]["Raf"].ToStr();

                if (CihazNo == 1)
                {
                    lblRaf1.Caption = "Raf 1: " + raf;
                    lblRaf1.Tag = Barcode;
                }
                else if (CihazNo == 2)
                {
                    lblRaf2.Caption = "Raf 2: " + raf;
                    lblRaf2.Tag = Barcode;
                }
                else if (CihazNo == 3)
                {
                    lblRaf3.Caption = "Raf 3: " + raf;
                    lblRaf3.Tag = Barcode;
                }
                else if (CihazNo == 4)
                {
                    lblRaf4.Caption = "Raf 4: " + raf;
                    lblRaf4.Tag = Barcode;
                }
            }
            else //Top barkodu
            {
                int shelfId = -1;

                if (CihazNo == 1)
                    shelfId = lblRaf1.Tag.ToInt();
                else if (CihazNo == 2)
                    shelfId = lblRaf2.Tag.ToInt();
                else if (CihazNo == 3)
                    shelfId = lblRaf3.Tag.ToInt();
                else if (CihazNo == 4)
                    shelfId = lblRaf4.Tag.ToInt();

                if (shelfId != -1)
                {
                    ExecuteNonQuery("UpdShelf", shelfId, Barcode);

                    DataRow[] Rolls = gvIrsaliye.Dataset().Tables[1].Select("RollId=" + Barcode.ToStr());
                    if (Rolls.Length > 0)
                        Rolls[0]["ShelfId"] = shelfId;
                }
            }
        }

        private void btnStart_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (biCom1.EditValue.ToInt() > 0)
                newLand.CihazEkle(1, biCom1.EditValue.ToInt());

            if (biCom2.EditValue.ToInt() > 0)
                newLand.CihazEkle(2, biCom2.EditValue.ToInt());

            if (biCom3.EditValue.ToInt() > 0)
                newLand.CihazEkle(3, biCom3.EditValue.ToInt());

            if (biCom4.EditValue.ToInt() > 0)
                newLand.CihazEkle(4, biCom4.EditValue.ToInt());

            btnStart.Enabled = false;
        }

        private void biCom_EditValueChanged(object sender, EventArgs e)
        {
            newLand.TumunuKapat();

            btnStart.Enabled = true;

            ConfExpert.Properties.Settings.Default.NewLand1 = biCom1.EditValue.ToInt();
            ConfExpert.Properties.Settings.Default.NewLand2 = biCom2.EditValue.ToInt();
            ConfExpert.Properties.Settings.Default.NewLand3 = biCom3.EditValue.ToInt();
            ConfExpert.Properties.Settings.Default.NewLand4 = biCom4.EditValue.ToInt();

            ConfExpert.Properties.Settings.Default.Save();
            ConfExpert.Properties.Settings.Default.Upgrade();
        }

        private void bbiGuncelle_ItemClick_1(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }
    }
}
